<template>
	<view>
			<button type="primary" class="btn-login" @click="userinfo">一键登录</button>
			 <button @click="getUserInfo()">获取用户登录信息</button>
			  <button @click="getUserInfoMethods()">获取用户登录信息</button>
			<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<image class="avatar" :src="userInfo.avatarUrl"></image>
			</button>
			<view>
				<button @click="getInfo()">clike me</button>
				<view class="">
					{{swiperList}}
				</view>
				<van-button type="default" @click="bt1()">登录</van-button>
				<button type="primary"  @click="getProduct()">主要按钮</button>
				<van-button type="info">信息按钮</van-button>
				<van-button type="warning">警告按钮</van-button>
				<van-button type="danger" @click="logout()">登出</van-button>
				{{user}}
			</view>
			
			<button @click="category()">categoryList</button>\
			<u-empty
					mode="car"
					icon="http://cdn.uviewui.com/uview/empty/car.png"
					width="320"
					height="320"
					textSize="28"
			>
			</u-empty>
			<u-empty
					mode="address"
					icon="http://cdn.uviewui.com/uview/empty/address.png"
					width="320"
					height="320"
					textSize="28"
			>
			</u-empty>
			<u-empty
					mode="order"
					icon="http://cdn.uviewui.com/uview/empty/order.png"
					width="320"
					height="320"
					textSize="28"
			>
			</u-empty>
			<u-empty
					mode="data"
					icon="http://cdn.uviewui.com/uview/empty/data.png"
					width="320"
					height="320"
					textSize="28"
			>
			</u-empty>

	</view>
	
</template>

<script>
	import {productList} from '../../api/products'
	import {mapState,mapActions} from 'vuex'
	import {categoryList} from '@/api/category.js'
	export default {
		data() {
			const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
			return {
				openid:'',
				userInfo: {
				  avatarUrl: defaultAvatarUrl,
				  nickName: '',
				},
				swiperList:{},
			}
		},
		computed:{
			user(){
				return this.$store.state.userToken
			}
		},
		methods: {
			category(){
				const config = {
					type : 1
				}
				categoryList(config).then(res=>{
					console.log(res)
				}).catch(err=>{
					console.log('err:'+err)
				})
			},
			userinfo(){
				let that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								console.log(infoRes);
								console.log(infoRes.userInfo);},
							fail() {
								console.log("xxx")
								}
							});
						},
						fail() {
							console.log("xxx")
							}
						});
			},
			getUserInfo() {
				let that = this
				uni.login({
					success (res) {
						console.log('login', res)
						// 通过uni.login获取到临时登录凭证code
						if (res.code) {
							//发起网络请求
							uni.request({
								url: 'https://api.weixin.qq.com/sns/jscode2session',
								data: {
									appid: '微信小程序appid',
									secret: '微信小程序密钥',
									js_code: res.code, // wx.login登录code
									grant_type: 'authorization_code' // 固定赋值
								},
								success(res) {
									console.log('res', res)
									that.openid = res.data.openid 
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			  onChooseAvatar(e) {
				  console.log(e)
			  },
			  async getInfo(){
			  	uni.request({
			  			url: 'http://localhost:8083/customer/category/list',
			  			data: {
			  				type:2
			  			},
			  			header: {
			  			  authentication: 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTUwODg3OTMsInVzZXJJZCI6MX0.AdRQ6RDRUAWUoWKBdeEkKIBrt7O5-aCPeagRcOEVdUM'
			  			},
			  			method: 'GET',
			  			success: (res) => {
			  			  console.log(res.data.data);
			  			  // this.arrType = res.data.data
			  			},
			  			fail: (error) => {
			  			  uni.showToast({
			  				title: '请求接口失败',
			  			  })
			  			  console.log(error);
			  			}
			  		  })
			  },
			  bt1(){
			  	wx.login({
			  		success: (res) => {
			  			console.log(res.code)
			  			// this.$store.state.userToken = res.code
			  			this.$store.dispatch('login',res.code)
			  			// login(res.code)
			  			
			  			console.log(this.$store.state.userToken)
			  		},
			  	})
			  },
			  logout(){
			  	this.$store.dispatch('logout')
			  },
			  getProduct(){
			  	const config={
			  		categoryId:2
			  	}
			  	productList(config).then(res=>{
			  		console.log(res)
			  	}).catch(err => {
			  		console.log(err)
			  	})
			  }
		}
	}
</script>

<style>

</style>
